package com.all.play.myapplication.fragment;

import android.graphics.Rect;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.all.play.myapplication.R;
import com.all.play.myapplication.base.BaseFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * Created by 聪明一只哈 on 2018/4/18.
 * Time: 2018/4/18  15:02
 */

public class ProviderFragment extends BaseFragment {
    @BindView(R.id.tv_goback)
    TextView tv_goback;
    @BindView(R.id.tv_tip)
    TextView tv_tip;
    @BindView(R.id.et_search_home)
    EditText etSearchHome;
    @BindView(R.id.tv_message_home)
    TextView tvMessageHome;
    @BindView(R.id.webview_show)
    WebView webviewShow;
    Unbinder unbinder;

    @Override
    public View initView() {
        View view = View.inflate(mContext, R.layout.fragment_provider, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {
        super.initData();
        Log.i("getNetData", "initData: " + "获得提供页数据");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.tv_goback, R.id.tv_message_home})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_goback:

                if (webviewShow!=null){
                    webviewShow.goBack();
                }
                break;
            case R.id.tv_message_home:
                searchclick();
                break;
        }
    }

    private void searchclick() {
        String tv_search = etSearchHome.getText().toString().trim();

        tv_tip.setVisibility(View.GONE);
        webviewShow.setVisibility(View.VISIBLE);

        if (tv_search != null && !TextUtils.isEmpty(tv_search)) {
            if (isSoftShowing()) {
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(INPUT_METHOD_SERVICE);

                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }

            webviewShow.loadUrl("https://www.baidu.com/s?wd=" + tv_search);


            //声明WebSettings子类
            WebSettings webSettings = webviewShow.getSettings();
            //设置自适应屏幕，两者合用
            // webview_game.setInitialScale(1);//这个很重要,但是在游戏界面,需要注释掉
            webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
            webSettings.setLoadWithOverviewMode(false); // 缩放至屏幕的大小  但是在游戏界面,需要false
            //缩放操作
            webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
            webSettings.setBuiltInZoomControls(false); //设置内置的缩放控件。若为false，则该WebView不可缩放
            webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
            webSettings.setAllowContentAccess(true);
            //支持通过JS打开新窗口
            webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
            //设置支持js交互  对这个游戏界面很重要
            webSettings.setJavaScriptEnabled(true);

            webSettings.setSaveFormData(true);

            //优先使用缓存:
            webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
            webviewShow.setLayerType(View.LAYER_TYPE_SOFTWARE, null);


            webviewShow.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    //这种方法解决重定向问题   按返回键,不起作用,死循环
                    WebView.HitTestResult hitTestResult = view.getHitTestResult();
                    //hitTestResult==null解决重定向问题
                    if (!TextUtils.isEmpty(url) && hitTestResult == null) {
                        view.loadUrl(url);
                        return true;
                    }
                    return false;
                }


            });
        } else {
            Toast.makeText(mContext, "搜索内容为空,请重新输入", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        webviewShow.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        webviewShow.onPause();

    }


    /**
     * 判断软键盘是否出现
     *
     * @author 聪明一只哈
     * @time 2018/3/3 17:17
     */
    private boolean isSoftShowing() {
        //获取当前屏幕内容的高度
        int screenHeight = getActivity().getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

        return screenHeight - rect.bottom != 0;
    }


}
